|
Viewing Source Code
|
Viewing the source code for an HTML document is the tried and tested way for a JavaScript hacker to learn the tricks of the trade. This method of learning, however, relies on the browser's ability to accurately display a document's source code. There are certain issues associated with different browser versions and, unfortunately, what you see cannot always be fully trusted.
From the list below, choose the browser you will be using in your Web content development for guidelines on viewing source code in that browser...
| Navigator 4.x |
For some reason, Netscape decided not to allow Navigator 4.x users to select their own text editor application for viewing HTML source code. This means no convenient loading directly into your Web development editor of choice. But, far more significant than this even, Navigator's default source viewer in 4.x doesn't even necessarily display the true source code of the document. In cases where a document uses JavaScript write and writeln statements to generate HTML, the built-in source viewer will display the generated HTML and not the JavaScript code that generated it. This can cause some content that is copied from the built-in source code viewer to break when pasted into new HTML documents. This problem renders the browser's built-in menu options for viewing source code practically useless to content developers who wish to learn how complex scripted pages accomplish their wizardry.
There are several ways to get around this problem with Navigator 4.x...
The "view-source:" protocol is somewhat of an undocumented feature of Netscape Navigator, which is also implemented in Navigator 3.0x. However, the source viewing system in Navigator 3.0x is so sweet that you would truly have no need to use it in this version. In Navigator 4.x, on the other hand, it's a lifesaver. It's quite simple to use.
If you are browsing a simple Web page that contains no framesets, then you can easily view the true source code for the page by simple prepending "view-source:" at the beginning of the URL in the browser's location field and then hitting the "Enter" button. This will open up the page's exact source code inside Navigator's built-in source viewer.
For example, if the HTML document you wished to view the source of was located at "http://www.some-domain.com", then you could view the exact source code for that page by adding "view-source:" to the beginning of this URL to create the new URL "view-source:http://www.some-domain.com". When you hit the "Enter" button, the source code for the homepage will appear.
You cannot save out from Navigator's source viewer, so the only way to get this source code into your editor of choice is to copy-and-paste the entire document. You can do this fairly easily by first pressing "Ctrl-A" ("Command-A" for Mac users), and then pressing "Ctrl-C" ("Command-C" for Mac users). Then, launch your editor and create a new document. Press "Ctrl-V" ("Command-V" for Mac users) to paste the contents of the clipboard into your new empty document.
Viewing Source Code Inside a Frameset
If you are trying to use "view-source:" to view the source code of an HTML page that is inside a frameset, then your life is made a little more complicated. In this case you will first need to find the URL for the frame you wish to view. This procedure is different for Navigator 4.x on Windows and Navigator 4.x on Mac...
Navigator 4.x on Windows
- Right-click on the frame you wish to view to bring up the contextual menu.
- Select the "Create Shortcut" option, which will open up a small dialog box.
- Click in the "URL" field and copy its contents to the clipboard. Once the cursor is inside this field, you can copy its contents quickly by pressing the "Home" button, then pressing "Shift-Ctrl-End", and then pressing "Ctrl-C" to copy to the clipboard.
- Start entering a new URL, beginning with "view-source:", into the browser's location field.
- Press "Ctrl-V" to paste the contents of the clipboard at the end of "view-source:".
- Press enter to open up the true source code for the HTML page using Navigator's built-in source viewer. You can copy-and-paste from the built-in source viewer, but you cannot easily save the entire document. Copy the entire contents by first pressing "Ctrl-A", and then pressing "Ctrl-C".
Navigator 4.x on Mac
- Make the frame active by clicking somewhere inside it, or by clicking on the scrollbar. If the frame contains many links and active objects, be careful not to click on these. You can safely click on the document's background, which is essentially "dead" space, and this will also make the frame active.
- Click on the "View" menu, to open up this menu.
- Select the "Page Info" option, which will open up a window with two frames.
- Carefully select and copy the "Location:" link, which you will find in the lower frame of the window.
- Close the "Page Info" window.
- Start entering a new URL, beginning with "view-source:", into the browser's location field.
- Press "Command-V" to paste the contents of the clipboard at the end of "view-source:".
- Press enter to open up the true source code for the HTML page using Navigator's built-in source viewer. You can copy-and-paste from the built-in source viewer, but you cannot easily save the entire document. Copy the entire contents by first pressing "Command-A", and then pressing "Command-C".
Saving the document you wish to view onto your local system is a fairly fuss-free way of viewing and manipulating source code, but it does run the risk of cluttering up your desktop (or other such area of your hard drive) with a plethora of files that you will inevitably forget whether you still need or not. So, you'll likely keep them hanging around and permit them to overstay their welcome.
If the document you are viewing is not in a frameset, then saving it out is very straightforward. Just follow these simple steps...
Saving a Document That's in a Frameset
To save a document that is inside a frameset, the procedure is slightly different between Navigator 4.x on Windows and Navigator 4.x on Mac,...
Navigator 4.x on Windows
- Make the frame active by clicking somewhere inside it, or by clicking on the scrollbar. If the frame contains many links and active objects, be careful not to click on these. You can safely click on the document's background, which is essentially "dead" space, and this will also make the frame active.
- Select the "Save Frame As..." option from the application File menu.
- Choose an appropriate filename and destination folder on your hard drive from the "Save As..." dialog that pops open. This is where your file will be saved.
- Make sure that you save the file as HTML and not "Plain Text", as the "Plain Text" option will not save the HTML source code for the page, but will instead save a text file that looks somewhat similar to how the document displayed in the browser.
- Click the "Save" button to save the page to the place you selected.
- Load the saved HTML file into your text editor of choice in order to view and/or edit the source code, and to possibly use all or just parts of it as a template for your own new Web pages.
Navigator 4.x on Mac
- Make the frame active by clicking somewhere inside it, or by clicking on the scrollbar. If the frame contains many links and active objects, be careful not to click on these. You can safely click on the document's background, which is essentially "dead" space, and this will also make the frame active.
- Select the "Save As..." option from the application File menu.
- Choose an appropriate filename and destination folder on your hard drive from the "Save As..." dialog that pops open. This is where your file will be saved.
- Make sure that you save the file as "Source" and not "Text", as the "Text" option will not save the HTML source code for the page, but will instead save a text file that looks somewhat similar to how the document displayed in the browser.
- Click the "Save" button to save the page to the place you selected.
- Load the saved HTML file into your text editor of choice in order to view and/or edit the source code, and to possibly use all or just parts of it as a template for your own new Web pages.
Be careful of framesets in Web sites. Quite often Web sites will hide their framesets as best as possible for aesthetic reasons by creating borderless frames. If you are trying to view the source code for a complex Web page and all you get by using the "View | Page Source" option is an inappropriately small document containing <FRAMESET> and <FRAME> tags, then the Web page you are viewing contains a frameset and you should first find out which frame in the frameset contains the code you would like to view.
Remember, if you are using Navigator 4.x and you wish to view source code that is accurate and that you can learn from or reuse in your own content, NEVER USE THE NAVIGATOR 4.x MENU OPTIONS TO VIEW HTML SOURCE.
| Internet Explorer 4.x and higher (Windows only) |
If you are using IE on Windows, you face a rather terrible predicament when it comes to viewing source code. Apparently, Microsoft has little interest in the HTML and JavaScript developers out there who learn by example. Internet Explorer takes source viewing one step further into the inferno of frustration than Netscape's Navigator 4.x does. Not only does IE not permit the user to register their own application for viewing source code, but the application that IE uses by default is not able to correctly display text files that are authored on Mac or Unix machines, or text editors for Windows which have the option of outputting Unix-style text files, for that matter.
I find myself setting my Windows text editor to Unix-style linebreaks out of habit, since most servers on the Web are Unix-based and there are also plenty of text editors for both Windows and Mac that can read and write Unix-style text files. On the other hand, there are few text editors for Windows that can deal with Mac-style text files, and few text editors for Mac that can understand DOS-style text files.
In IE's defense, however, even though Notepad is not able to deal very gracefully with Unix or Mac linebreaks, it is attempting in its own endearing way to show the actual true source code for the HTML page, and not the generated product of JavaScript write and writeln statements. And if you save a document from Notepad or Wordpad and republish it to the Web, it will still remain fully functional, if not very pretty to work with. Don't let the lines that run straight into each other, or the ugly solid blocks bother you. Those blocks are the linebreaks that Notepad chokes on.
There's only one way that I can advocate for safely viewing HTML source code in Internet Explorer on Windows...
Saving the document you wish to view onto your local system is a fairly fuss-free way of viewing and manipulating source code, but it does run the risk of cluttering up your desktop (or other such area of your hard drive) with a plethora of files that you will inevitably forget whether you still need or not. So, you'll likely keep them hanging around and permit them to overstay their welcome.
If the document you are viewing is not in a frameset, then saving it out is very straightforward. Just follow these simple steps...
To save a document that is inside a frameset, you will need to use Internet Explorer's "View Source" option as follows...